package com.tao.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tao.entity.User;
import com.tao.service.LoginService;


/**
 * 登陆的控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/servlet")
public class LoginController {

	@Autowired
	private LoginService loginService;
	
	@RequestMapping(value={"/LoginServlet"},params={"phone","pwd"},method=RequestMethod.GET)
	@ResponseBody     //向前台传输数据
	public int login(
			@RequestParam(value="phone") String phone,
			@RequestParam(value="pwd") String pwd,
			HttpSession session,
			HttpServletResponse response){
		//注意前面提交的数据的name 必须是和实体中的属性名，相匹配，否则获取不到值，并且有相应参数的构造函数
		
		User user=new User();
		user.setPhone(phone);
		user.setPwd(pwd);
		
		System.out.println("手机号:"+user.getPhone());
		System.out.println("密码:  "+user.getPwd());
		int s=0;
			int flag=loginService.Login(user);
			if (flag==1){
			     s=-1;
				System.out.print("用户名错误");
			}else if(flag==2){
			     s=-2;
				System.out.print("密码错误") ; 
			}else if(flag==3){
			      s=user.getId();  //传回userid
				session.setAttribute("user", user);
				System.out.print("登陆成功");    
			}
			  
		return s;
	}
	
	
}
